package com.caiyi.financial.nirvana.sms.mapper;


import com.caiyi.financial.nirvana.sms.base.mapper.BaseMapper;
import com.caiyi.financial.nirvana.sms.entity.SmsUserRecharge;
import com.caiyi.financial.nirvana.sms.querybean.UserRechargeQueryBean;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;

/**
 * 客户充值记录MAPPER
 */
@Mapper
public interface CustomerRechargeMapper extends BaseMapper<SmsUserRecharge> {

    @Select("select sum(sms_num) from sms_user_recharge where account = #{account}")
    Integer getAccountSmsNum(@Param("account") String account);

    @SelectProvider(type = UserSqlProvider.class, method = "selectUserRechargeListByParams")
    List<SmsUserRecharge> selectUserRechargeListByParams(UserRechargeQueryBean bean);

    class UserSqlProvider {
        public String selectUserRechargeListByParams(UserRechargeQueryBean bean) {
            return new SQL() {
                {
                    SELECT("id, account,type,sms_num ,before_num,after_num,description,create_time,update_time,order_num");
                    FROM("sms_user_recharge");
                    if (!StringUtils.isEmpty(bean.getAccount())) {
                        WHERE("account LIKE concat('%',#{account},'%')");
                    }
                    if (!StringUtils.isEmpty(bean.getOrderNum())) {
                        WHERE("order_num=#{orderNum}");
                    }
                    if (bean.getType() != null) {
                        WHERE("type=#{type}");
                    }
                    if (bean.getStartTime() != null) {
                        WHERE("create_time >= #{startTime}");
                    }
                    if (bean.getEndTime() != null) {
                        WHERE("create_time <= date_add(#{endTime}, interval 1 day)");
                    }
                    ORDER_BY("id desc");
                }
            }.toString();
        }
    }
}
